package com.hoyo.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.hoyo.HoyoApplication;
import com.hoyo.common.PageInfo;
import com.hoyo.config.UserRealm.Principal;
import com.hoyo.domain.Bullet;
import com.hoyo.domain.Catalog;
import com.hoyo.domain.Channel;
import com.hoyo.domain.IndexPageChannel;
import com.hoyo.domain.User;
import com.hoyo.domain.UserInfo;
import com.hoyo.service.CatalogService;
import com.hoyo.service.ChannelService;
import com.hoyo.service.RouteService;
import com.hoyo.service.UserService;

@Controller
public class RouteController {
	@Autowired
	private UserService userService;
	
	@Autowired
	private RouteService routeService;
	
	@Autowired
	private ChannelService channelService;
	
	@Autowired
	private CatalogService catalogService;
	
	/**
	 * 获取主页展示对应频道的top10 、top7
	 * @return
	 * AnnotationConfigEmbeddedWebApplicationContext
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value={"", "/", "index"})
	public ModelAndView index(ModelAndView model) throws UnsupportedEncodingException {
		 model.setViewName("index.html");
		 try {
			 List<IndexPageChannel> channels = routeService.indexPrepared();
			 model.addObject("channels", channels);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return model;
	}
	
	@RequestMapping("/catalog/{catalogId}")
	public ModelAndView openCatalog(@PathVariable("catalogId") String catalogId,HttpSession session){
		ModelAndView mav = new ModelAndView("movie.html");
		try {
			
			System.out.println("catalogId:" + catalogId);
			Catalog catalog = routeService.catalogPrepared(Integer.valueOf(catalogId));
			List<Channel> channels = channelService.findAllChannelsAndCountMovieInEveryChannel();
			User user = (User) session.getAttribute("user");
			if(user!=null){
				if(catalogService.findUserisLikedCatalogByUserId(Integer.parseInt(catalogId), user.getId()).size()>0){
					mav.addObject("isliked", true);
				}else{
					mav.addObject("isliked", false);
				}
			}
			mav.addObject("channels", channels);
			mav.addObject("catalog", catalog);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	@RequestMapping("/admin")
	public ModelAndView admin(ModelAndView mav){
		mav.setViewName("admin.html");
		
		try {
			JSONObject obj = routeService.adminPrepared();
			mav.addObject("countInfo", obj.get("countInfo"));
			mav.addObject("catalogs", obj.get("catalogs"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}
	
	@RequestMapping("/admin/comment")
	public ModelAndView comment(ModelAndView mav){
		mav.setViewName("comment_manage.html");
		
		return mav;
	}
	
	@RequestMapping("/admin/bullet")
	public ModelAndView bullet(ModelAndView mav){
		mav.setViewName("danmu_manage.html");
		
		return mav;
	}
	
	@RequestMapping("/admin/movie")
	public ModelAndView movie(ModelAndView mav){
		mav.setViewName("movie_manage.html");
		try {
			List<Channel> channels = channelService.findAllChannels();
			mav.addObject("channels", channels);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	@RequestMapping("/admin/upload")
	public String upload(){
		return "checkupload.html";
	}
	
	@RequestMapping("/admin/report")
	public String report(){
		return "report_manage.html";
	}
	
	@RequestMapping("/admin/channel")
	public String channel(){
		return "channel_manage.html";
	}
	
	@RequestMapping("/admin/catalog")
	public String catalog(){
		return "catalogReport_manage.html";
	}
	
	/**
	 * unauthor
	 * @return
	 */
	@RequestMapping("/unauthor")
	public String unauthor() {
		return "unauthor.html";
	}
	
	/**
	 * reports
	 * @return
	 */
	@RequestMapping("/reports")
	public ModelAndView reports() {
		return new ModelAndView("reports.html");
	}
	
	@RequestMapping("/403")
	public ModelAndView unauthorized() {
		return new ModelAndView("unauthorized.html");
	}
	
	@RequestMapping("/500")
	public String exception() {
		return "500.html";
	}
	
	@RequestMapping("/404")
	public String pageNotFound() {
		return "404.html";
	}
	
	@RequestMapping("/register")
	public ModelAndView register() throws Exception{
		ModelAndView mav = new ModelAndView("reg.html");
		return mav;
	}
}
